package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author hql
 * @date 2019-12-05 12:17
 */
@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceImpl  implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    /**
     * 将上传的Excel数据添加或者修改到数据库中
     * @param demoData
     * @return
     */
    @Override
    public void addOrUpdate(List<OrderSetting> demoData) {
        if (demoData!=null){
            for (OrderSetting data : demoData) {
                //判断是否存在
                Date date = data.getOrderDate();
                Integer count =ordersettingDao.ifHave(date);
                if (count>0){
                    ordersettingDao.updateOrdersetting(data);
                }else {
                    ordersettingDao.addOrdersetting(data);
                }
            }

        }

     }

    /**
     * 查询所有的预约内容
     * @param date
     * @return
     */
    @Override
    public List<Map> findAllOrdersetting(String date) {
        //根据前台的需求将时间格式的字符串设置为date:1 格式 采用map集合封装数据
        String begin = date + "-"+1;
        String end = date + "-"+31;


        List<OrderSetting> orderSettingList = ordersettingDao.findAllOrdersetting(begin,end);
        List<Map> changeOrderSettingList= new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String,Integer> map = new LinkedHashMap<>();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            changeOrderSettingList.add(map);
        }

        return changeOrderSettingList;
    }

    /**
     * 添加或者修改所需要的预约人数
     * @param orderSetting
     */
    @Override
    public void changeNumber(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        Integer count = ordersettingDao.ifHaveOrderDate(orderDate);
      if (count>0){
          ordersettingDao.updateNumber(orderSetting);
      }else {
          ordersettingDao.addOrder(orderSetting);
      }

    }

    /**
     * 判断当天日期是否能够进行预约
     * @param orderDate
     * @return
     */
    @Override
    public OrderSetting findByOrderSetting(String orderDate) {
        OrderSetting orderSetting= ordersettingDao.findOrdersetting(orderDate);

        return orderSetting;
    }

    /**
     * 更新预约信息
     * @param orderSetting
     */
    @Override
    public void updateOrderSetting(OrderSetting orderSetting) {
        ordersettingDao.update2(orderSetting);
    }
}
